package com.young.jdbc.myjdbc;

import java.sql.*;

/**
 * @Author: young
 * @Date: 2021-12-03 10:16
 * @Description:演示JdbcUtils封装的工具类的使用
 */
public class JdbcUtils_Use {
    public static void main(String[] args) {
        JdbcUtils_Use jdbcUtils_use = new JdbcUtils_Use();
        jdbcUtils_use.testSelect();
    }
    public void testDML(){
        // 得到连接
        Connection connection = null;

        // 执行SQL语句
        String sql = "update student set name=? where id=?";
        PreparedStatement preparedStatement = null;
        try {
            connection = JdbcUtils.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,"哈哈");
            preparedStatement.setInt(2,1);
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            JdbcUtils.close(null,preparedStatement,connection);
        }
    }
    public void testSelect(){
        // 得到连接
        Connection connection = null;

        // 执行SQL语句
        String sql = "select * from student";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = JdbcUtils.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while(resultSet.next()){
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                String sex = resultSet.getString(3);
                Date birthday = resultSet.getDate(4);
                String mobile = resultSet.getString(5);
                System.out.println(id+"\t"+name+"\t"+sex+"\t"+birthday+"\t"+mobile);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            JdbcUtils.close(resultSet,preparedStatement,connection);
        }
    }
}
